package cn.yunhe.validator;

import org.springframework.stereotype.Controller;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/*Spring注解/SpringMVC注解*/
@Controller
public class OrderController {


    //直接点击如下链接可跳转||
    //http://localhost:8080/OrderInfoValidate111.dojava
    //页面提交表单===
    //http://localhost:8080/ZViewTest/commit2OrderInfo.jsp
    @RequestMapping(value = "/OrderInfoValidate111")
    public ModelAndView OrderInfoValidate(@Valid Order order, Errors errors){

        System.out.println("getProductId====="+order.getProductId());
        System.out.println("getPrice====="+order.getPrice());
        System.out.println("getQuantity====="+order.getQuantity());
        System.out.println("getAmount====="+order.getAmount());

        //是否存在错误
        if (errors.hasErrors()){
            List<FieldError> fieldErrorList= errors.getFieldErrors();
            for (FieldError fieldError:fieldErrorList){
                //打印错误信息
                System.out.println("field==="+fieldError.getField()+"\t defaultMessage====="+fieldError.getDefaultMessage());
            }
        }

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;

    }


    @InitBinder
    public void initalizationBinder(DataBinder dataBinder){
        dataBinder.setValidator(new OrderValidator());
    }
}
